﻿Imports System.Data
Imports VSModel
Imports Cls_Common
Imports SecurityService
Partial Class Control_Raovat_Create
    Inherits System.Web.UI.UserControl
    Dim level As String = -1
    Private Shared ReadOnly log As log4net.ILog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)

#Region "Sub and Function "

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        If Session("UserId") Is Nothing Then
            Response.Redirect(MainDomain & "thanh-vien.html")
            'Else

            '    If Session("SoLanThemTin") Is Nothing Or Session("SoLanThemTin") < 1 Then
            '        Excute_Javascript("Số tin bạn được thêm đã hết, vui lòng kiểm tra", Me.Page)
            '        Response.Redirect(MainDomain & "thong-bao-tin-dang.html")
            '    End If
        End If
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then

            LoadType()
            TypeThongTinCoBan()
            TypeThongTinKyThuat()
            getYear()
            LoadUser()
            Dim script As ScriptManager = ScriptManager.GetCurrent(Me.Page)
            If Not script Is Nothing AndAlso script.IsInAsyncPostBack Then
                ScriptManager.RegisterStartupScript(Me.Page, Me.Page.GetType, "duyjs", "ajaxJquery()", True)
            Else
                Me.Page.ClientScript.RegisterStartupScript(Me.Page.GetType(), "duyjs", String.Concat("Sys.Application.add_load(function(){", "ajaxJquery()", "});"), True)
            End If

        End If
    End Sub

    Protected Sub LoadType()
        Using data As New VSEntities
            'Khu vuc
            ddlKhuVuc.AppendDataBoundItems = True
            ddlKhuVuc.Items.Insert(0, New ListItem("---Chọn---", "0"))
            Dim ba = (From ca In data.DonViHanhChinhs Select ca.Id, ca.Title).ToList
            ddlKhuVuc.DataTextField = "Title"
            ddlKhuVuc.DataValueField = "ID"
            ddlKhuVuc.DataSource = ba
            ddlKhuVuc.DataBind()
        End Using
    End Sub

    Protected Sub LoadUser()
        Using data As New VSEntities
            Dim intUser As Integer = Session("UserID")
            Dim p = (From q In data.Users Where q.UserId = intUser Select q).FirstOrDefault
            If Not p Is Nothing Then
                txtHoTen.Text = p.FullName
                txtDiaChi.Text = p.DiaChi
                txtDienThoai.Text = p.Phone
                txtDienThoaiKhac.Text = p.MobileOrther
                ddlKhuVuc.SelectedValue = p.TinhTP

            End If
            'kiem tra neu la thanh vien thuong va so dien thoai da co tin thi ko cho dang nua, IsUser= 5 la thanh vien thuong
            'Dim ktra = (From q In data.RaoVats Where q.User.Phone = p.Phone And q.User.IsUser = 5 Select q).FirstOrDefault
            'If Not ktra Is Nothing AndAlso Session("UserVip") Is Nothing Then
            '    lblThongBao.Text = "Tài khoản với điện thoại " & p.Phone & " của bạn đã  tạo tin đăng bán, để thêm tin mới vui lòng liên hệ với quản trị"
            '    btnSave.Enabled = False
            'End If

        End Using
    End Sub

    Protected Sub getDoiXe(ByVal intParent As Integer)
        ddlDoixe.Items.Clear()
        ddlDoixe.AppendDataBoundItems = True
        ddlDoixe.Items.Insert(0, New ListItem("---Chọn---", "0"))
        Using data As New VSEntities
            Dim query As List(Of Category) = (From q In data.Categories Where q.ParentId = intParent Order By q.Sort Ascending Select q).ToList
            ddlDoixe.DataValueField = "CatId"
            ddlDoixe.DataTextField = "Title"
            ddlDoixe.DataSource = query
            ddlDoixe.DataBind()
        End Using
    End Sub

    Private Sub RecursiveFillTree(ByVal dtParent As List(Of Category), ByVal parentID As Integer)
        level += 1
        'on the each call level increment 1
        Dim appender As New StringBuilder()

        For j As Integer = 0 To level - 1

            appender.Append("&nbsp;&nbsp;&nbsp;&nbsp;")
        Next
        If level > 0 Then
            appender.Append("|__")
        End If

        Using data As New VSEntities
            Dim dv As List(Of Category) = (From q In data.Categories Where q.ParentId = parentID Select q).ToList
            Dim i As Integer
            If dv.Count > 0 Then
                For i = 0 To dv.Count - 1
                    Dim itm As New ListItem(Server.HtmlDecode(appender.ToString() + dv.Item(i).Title.ToString()), dv.Item(i).CatId.ToString())
                    ddlDoixe.Items.Add(itm)
                    RecursiveFillTree(dtParent, Integer.Parse(dv.Item(i).CatId.ToString()))
                Next
            End If
        End Using
        level -= 1
        'on the each function end level will decrement by 1
    End Sub

    Protected Sub getYear()
        ddlNamsanxuat.AppendDataBoundItems = True
        ddlNamsanxuat.Items.Add(New ListItem("---Chọn---", 0))
        ddlNamsanxuat.DataSource = Enumerable.Range(1975, 50)
        ddlNamsanxuat.DataBind()
        ddlNamsanxuat.SelectedValue = Now.Year
    End Sub

    ''' <summary>
    '''Lay thong so tu OtherList 
    ''' </summary>
    ''' <remarks></remarks>
    ''' 

    Private Sub TypeThongTinCoBan()
        Using Data As New VSEntities
            'Xuat xu
            ddlXuatXu.AppendDataBoundItems = True
            ddlXuatXu.Items.Insert(0, New ListItem("---Chọn---", "0"))
            Dim po = (From mu In Data.vXuatXus Select mu).ToList
            ddlXuatXu.DataTextField = "NAME"
            ddlXuatXu.DataValueField = "ID"
            ddlXuatXu.DataSource = po
            ddlXuatXu.DataBind()

            'Hang san xuat
            ddlHangchetao.AppendDataBoundItems = True
            ddlHangchetao.Items.Insert(0, New ListItem("---Chọn---", "0"))
            Dim hk = (From lk In Data.Categories Where lk.ParentId = 0 Select lk.CatId, lk.Title).ToList
            ddlHangchetao.DataTextField = "Title"
            ddlHangchetao.DataValueField = "CatId"
            ddlHangchetao.DataSource = hk
            ddlHangchetao.DataBind()
            'Doi xe
            ddlDoixe.Items.Clear()
            ddlDoixe.AppendDataBoundItems = True
            ddlDoixe.Items.Insert(0, New ListItem("---Chọn---", "0"))

            'Doi xe
            ddlDoixe.AppendDataBoundItems = True
            ddlDoixe.Items.Insert(0, New ListItem("---Chọn---", "0"))
            Dim ho = (From tk In Data.vDoiXes Select tk).ToList
            ddlDoixe.DataTextField = "NAME"
            ddlDoixe.DataValueField = "ID"
            ddlDoixe.DataSource = ho
            ddlDoixe.DataBind()

            'Dong xe
            ddlDongXe.AppendDataBoundItems = True
            ddlDongXe.Items.Insert(0, New ListItem("---Chọn---", "0"))
            Dim t = (From k In Data.vDongXes Select k).ToList
            ddlDongXe.DataTextField = "NAME"
            ddlDongXe.DataValueField = "ID"
            ddlDongXe.DataSource = t
            ddlDongXe.DataBind()

            'Tinhtrang
            ddlTinhTrang.AppendDataBoundItems = True
            ddlTinhTrang.Items.Insert(0, New ListItem("---Chọn---", "0"))
            Dim e = (From v In Data.vTinhTrangs Select v).ToList
            ddlTinhTrang.DataTextField = "NAME"
            ddlTinhTrang.DataValueField = "ID"
            ddlTinhTrang.DataSource = e
            ddlTinhTrang.DataBind()

            'Mau ngoai that xe
            ddlMauNgoaiThat.AppendDataBoundItems = True
            ddlMauNgoaiThat.Items.Insert(0, New ListItem("---Chọn---", "0"))
            Dim f = (From d In Data.vMauNgoaiThats Select d).ToList
            ddlMauNgoaiThat.DataTextField = "NAME"
            ddlMauNgoaiThat.DataValueField = "ID"
            ddlMauNgoaiThat.DataSource = f
            ddlMauNgoaiThat.DataBind()

            'Mau noi that xe
            ddlMauNoiThat.AppendDataBoundItems = True
            ddlMauNoiThat.Items.Insert(0, New ListItem("---Chọn---", "0"))
            Dim u = (From s In Data.vMauNoiThats Select s).ToList
            ddlMauNoiThat.DataTextField = "NAME"
            ddlMauNoiThat.DataValueField = "ID"
            ddlMauNoiThat.DataSource = u
            ddlMauNoiThat.DataBind()

            'Nhien lieu
            ddlNhienLieu.AppendDataBoundItems = True
            ddlNhienLieu.Items.Insert(0, New ListItem("---Chọn---", "0"))
            Dim a = (From b In Data.vNhienLieux Select b).ToList
            ddlNhienLieu.DataTextField = "NAME"
            ddlNhienLieu.DataValueField = "ID"
            ddlNhienLieu.DataSource = a
            ddlNhienLieu.DataBind()

            'He Thong Nhien lieu
            ddlHeThongNhienLieu.AppendDataBoundItems = True
            ddlHeThongNhienLieu.Items.Insert(0, New ListItem("---Chọn---", "0"))
            Dim y = (From c In Data.vHeThongNhienLieux Select c).ToList
            ddlHeThongNhienLieu.DataTextField = "NAME"
            ddlHeThongNhienLieu.DataValueField = "ID"
            ddlHeThongNhienLieu.DataSource = y
            ddlHeThongNhienLieu.DataBind()

            'Hop so
            ddlHopSo.AppendDataBoundItems = True
            ddlHopSo.Items.Insert(0, New ListItem("---Chọn---", "0"))
            Dim n = (From m In Data.vHopSoes Select m).ToList
            ddlHopSo.DataTextField = "NAME"
            ddlHopSo.DataValueField = "ID"
            ddlHopSo.DataSource = n
            ddlHopSo.DataBind()

            'Dan dong
            ddlDanDong.AppendDataBoundItems = True
            ddlDanDong.Items.Insert(0, New ListItem("---Chọn---", "0"))
            Dim j = (From o In Data.vDanDongs Select o).ToList
            ddlDanDong.DataTextField = "NAME"
            ddlDanDong.DataValueField = "ID"
            ddlDanDong.DataSource = j
            ddlDanDong.DataBind()

        End Using
    End Sub

    Private Sub TypeAnToan()

    End Sub

    Private Sub TypeTienNghi()

    End Sub

    Private Sub TypeThongTinKyThuat()
        Using Data As New VSEntities

            'Dong co
            ddlDongCo.AppendDataBoundItems = True
            ddlDongCo.Items.Insert(0, New ListItem("---Chọn---", "0"))
            Dim t = (From k In Data.vDongCoes Select k).ToList
            ddlDongCo.DataTextField = "NAME"
            ddlDongCo.DataValueField = "ID"
            ddlDongCo.DataSource = t
            ddlDongCo.DataBind()

            'Kieu Dong co
            ddlKieuDongCo.AppendDataBoundItems = True
            ddlKieuDongCo.Items.Insert(0, New ListItem("---Chọn---", "0"))
            Dim n = (From h In Data.vKieuDongCoes Select h).ToList
            ddlKieuDongCo.DataTextField = "NAME"
            ddlKieuDongCo.DataValueField = "ID"
            ddlKieuDongCo.DataSource = n
            ddlKieuDongCo.DataBind()

            'Phanh
            ddlPhanh.AppendDataBoundItems = True
            ddlPhanh.Items.Insert(0, New ListItem("---Chọn---", "0"))
            Dim u = (From h In Data.vPhanhs Select h).ToList
            ddlPhanh.DataTextField = "NAME"
            ddlPhanh.DataValueField = "ID"
            ddlPhanh.DataSource = u
            ddlPhanh.DataBind()

            'Giam soc
            ddlGiamSoc.AppendDataBoundItems = True
            ddlGiamSoc.Items.Insert(0, New ListItem("---Chọn---", "0"))
            Dim l = (From r In Data.vPhanhs Select r).ToList
            ddlGiamSoc.DataTextField = "NAME"
            ddlGiamSoc.DataValueField = "ID"
            ddlGiamSoc.DataSource = l
            ddlGiamSoc.DataBind()

            'Vanh mam
            ddlVanhMam.AppendDataBoundItems = True
            ddlVanhMam.Items.Insert(0, New ListItem("---Chọn---", "0"))
            Dim g = (From h In Data.vVanhMams Select h).ToList
            ddlVanhMam.DataTextField = "NAME"
            ddlVanhMam.DataValueField = "ID"
            ddlVanhMam.DataSource = g
            ddlVanhMam.DataBind()
            'Lop xe
            ddlLopXe.AppendDataBoundItems = True
            ddlLopXe.Items.Insert(0, New ListItem("---Chọn---", "0"))
            Dim z = (From x In Data.vLopXes Select x).ToList
            ddlLopXe.DataTextField = "NAME"
            ddlLopXe.DataValueField = "ID"
            ddlLopXe.DataSource = z
            ddlLopXe.DataBind()
        End Using
    End Sub

    ''' <summary>
    ''' Insert Thong tin co ban
    ''' </summary>
    ''' <remarks></remarks>
    ''' 

    Private Sub InsertThongTinCoBan(ByVal intRaoVatId As Integer)
        Using data As New VSEntities
            Dim p As New VSModel.ThongSoCoBan
            Try
                p.HangCheTao = ddlHangchetao.SelectedValue
                p.NamSanXuat = ddlNamsanxuat.SelectedValue
                p.DongXe = ddlDongXe.SelectedValue
                p.DoiXe = ddlDoixe.SelectedValue
                p.TinhTrang = ddlTinhTrang.SelectedValue
                p.XuatXu = ddlXuatXu.SelectedValue
                p.SoKLMDaDi = txtSoKm.Text.Replace(",", "").Trim()
                p.MauNgoaiThat = ddlMauNgoaiThat.SelectedValue
                p.MauNoiThat = ddlMauNoiThat.SelectedValue
                If Not String.IsNullOrEmpty(txtSocua.Text.Trim) = True Then
                    p.SoCua = txtSocua.Text.Replace(",", "").Trim()
                End If
                If Not String.IsNullOrEmpty(txtSochongoi.Text.Trim) = True Then
                    p.SoChoNgoi = txtSochongoi.Text.Replace(",", "").Trim()
                End If
                p.NhienLieu = ddlNhienLieu.SelectedValue
                p.HeThongNhienLieu = ddlHeThongNhienLieu.SelectedValue
                p.HopSo = ddlHopSo.SelectedValue
                p.DanDong = ddlDanDong.SelectedValue
                If Not String.IsNullOrEmpty(txtTieuthunhienlieu.Text.Trim) = True Then
                    p.TieuThuNhienLieu = txtTieuthunhienlieu.Text.Replace(",", "").Trim()
                End If
                p.Description = txtThietBiKhacCoBan.Text.Trim()
                p.RaoVatId = intRaoVatId
                p.PhanHangModel = txtPhanHangModel.Text.Trim



                data.ThongSoCoBans.AddObject(p)
                data.SaveChanges()
                ' Insert_App_Log("Insert Advertise:" & txtTitle.Text.Trim & "", Function_Name.Advertisement, Audit_Type.Create, Request.ServerVariables("REMOTE_ADDR"), Session("UserName"))
                ' Excute_Javascript("Alertbox('Cập nhật dữ liệu thành công.');window.location ='../../Page/Raovat/Step2.aspx?RaovatId=" + hidRaoVatId.Value + "';", Me.Page, True)
            Catch ex As Exception
                log4net.Config.XmlConfigurator.Configure()
                log.Error("Error error " & AddTabSpace(1) & Session("Username") & AddTabSpace(1) & "IP:" & GetIPAddress(), ex)
                Excute_Javascript("Alertbox('Cập nhật thất bại.');", Me.Page, True)
            End Try
        End Using
    End Sub

    ''' <summary>
    ''' Insert An toan
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub InsertAnToan(ByVal intRaoVatId As Integer)
        Using data As New VSEntities
            Dim p As New VSModel.AnToan
            Try
                p.TuiKhiNguoiLai = rptTuiKhiNguoiLai.Checked
                p.TuiKhiTruoc = rptTuiKhiTruoc.Checked
                p.TuiKhiSau = rptTuiKhisau.Checked
                p.TuiKhiHaiBenGhe = rptHaiBenGhe.Checked
                p.PhanhABS = rptABS.Checked
                p.PhanBoLucABD = rptPhanhDienTu.Checked
                p.TroLucPhanhEBA = rptEBA.Checked
                p.CanBangDienTuESP = rptESB.Checked
                p.DieuKhienHanhTrinh = rptDKHanhTrinh.Checked
                p.HoTroCanhBaoLui = rptCanhBaoLui.Checked
                p.KiemSoatTruot = rptKiemSoatTruot.Checked
                p.ChotCuaAnToan = rptChotAntoan.Checked
                p.KhoaCuaTuDong = rptKhoaCuaTuDong.Checked
                p.KhoaCuaDKTuXa = rptKhoaCuaDKTuXa.Checked
                p.KhoaDongCo = rptKhoaDongCo.Checked
                p.HTBaoNgoaiVi = rptChongTrom.Checked
                p.DenSuongMu = rptDenSuongMu.Checked
                p.DenDayAnToan = rptThatDayAnToan.Checked
                p.DenPhanhPhu3 = rptDenPhu.Checked
                p.RaoVatId = intRaoVatId
                data.AnToans.AddObject(p)
                data.SaveChanges()
                'Insert_App_Log("Insert Advertise:" & txtTitle.Text.Trim & "", Function_Name.Advertisement, Audit_Type.Create, Request.ServerVariables("REMOTE_ADDR"), Session("UserName"))
                '  Excute_Javascript("Alertbox('Cập nhật dữ liệu thành công.');window.location ='../../Page/Raovat/Step3.aspx?RaovatId=" + hidRaoVatId.Value + "';", Me.Page, True)
            Catch ex As Exception
                log4net.Config.XmlConfigurator.Configure()
                log.Error("Error error " & AddTabSpace(1) & Session("Username") & AddTabSpace(1) & "IP:" & GetIPAddress(), ex)
                Excute_Javascript("Alertbox('Cập nhật thất bại.');", Me.Page, True)
            End Try

        End Using
    End Sub

    ''' <summary>
    ''' Insert Tien nghi
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub InsertTienNghi(ByVal intRaoVatId As Integer)
        Using data As New VSEntities
            Dim p As New VSModel.TienNghi
            Try
                p.ThietBiDinhVi = rptThietBiDinhVi.Checked
                p.CuaSoNoc = rptCuaSoNoc.Checked
                p.KinhChinhDien = rptKinhChinhDien.Checked
                p.TayLaiTroLuc = rptTayLaiTroLuc.Checked
                p.GheChatLieuTienNghi = txtGheChatLieu.Text.Trim()
                p.DieuHoaTruoc = rptDieuHoaTruoc.Checked
                p.DieuHoaSau = rptDieuHoaSau.Checked
                p.HoTroXeTuDong = rptHoTroXeTuDong.Checked
                p.SayKinhSau = rptSaykinhsau.Checked
                p.QuatKinhPhiaSau = rptQuatKinhSau.Checked
                p.KinhMau = rptKinhMau.Checked
                p.ManHinhLCD = rptLCD.Checked
                p.ThietBiGiaiTri = rptGiaiTri.Checked
                p.ThietBiKhac = txtThietBiKhacTienNghi.Text.Trim
                p.RoaVatId = intRaoVatId
                data.TienNghis.AddObject(p)
                data.SaveChanges()

                'Insert_App_Log("Insert Advertise:" & txtTitle.Text.Trim & "", Function_Name.Advertisement, Audit_Type.Create, Request.ServerVariables("REMOTE_ADDR"), Session("UserName"))
                '  Excute_Javascript("Alertbox('Cập nhật dữ liệu thành công.');window.location ='../../Page/Raovat/Step4.aspx?RaovatId=" + hidRaoVatId.Value + "';", Me.Page, True)
            Catch ex As Exception
                log4net.Config.XmlConfigurator.Configure()
                log.Error("Error error " & AddTabSpace(1) & Session("Username") & AddTabSpace(1) & "IP:" & GetIPAddress(), ex)
                Excute_Javascript("Alertbox('Cập nhật thất bại.');", Me.Page, True)
            End Try
        End Using
    End Sub

    ''' <summary>
    ''' Insert Thong so ky thuat
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub InsertThongSoKyThuat(ByVal intRaoVatId As Integer)
        Using data As New VSEntities
            Dim p As New VSModel.ThongSoKyThuat
            Try
                p.DaiRongCao = txtDaiRongCao.Text.Trim()


                If Not String.IsNullOrEmpty(txtChieudaicoso.Text.Trim()) = True Then
                    p.ChieuDaiCoSo = txtChieudaicoso.Text.Replace(",", "").Trim()

                End If
                If Not String.IsNullOrEmpty(txtChieurongcoso.Text.Trim()) = True Then
                    p.ChieuRongCoSo = txtChieurongcoso.Text.Replace(",", "").Trim()
                End If

                If Not String.IsNullOrEmpty(txtTrongluongkhongtai.Text.Trim()) = True Then
                    p.TrongLuongKhongTai = txtTrongluongkhongtai.Text.Replace(",", "").Trim()
                End If

                If Not String.IsNullOrEmpty(txtDungtichbinhnhienlieu.Text.Trim()) = True Then
                    p.DungTichBinhNhienLieu = txtDungtichbinhnhienlieu.Text.Replace(",", "").Trim()
                End If
                p.DongCo = ddlDongCo.SelectedValue
                p.KieuDongCo = ddlKieuDongCo.SelectedValue
                If Not String.IsNullOrEmpty(txtDungtichxilanh.Text.Trim()) = True Then
                    p.DungTichXiLanh = txtDungtichxilanh.Text.Replace(",", "").Trim()
                End If

                p.Phanh = ddlPhanh.SelectedValue
                p.GiamSoc = ddlGiamSoc.SelectedValue
                p.LopXe = ddlLopXe.SelectedValue
                p.VanhMam = ddlVanhMam.SelectedValue
                p.ThongSoKhac = txtThietBiKhacKyThuat.Text.Trim()
                p.RaoVatId = intRaoVatId
                data.ThongSoKyThuats.AddObject(p)
                data.SaveChanges()
                'Insert_App_Log("Insert Advertise:" & txtTitle.Text.Trim & "", Function_Name.Advertisement, Audit_Type.Create, Request.ServerVariables("REMOTE_ADDR"), Session("UserName"))
                '   Excute_Javascript("Alertbox('Cập nhật dữ liệu thành công.');window.location ='../../Page/Raovat/List.aspx';", Me.Page, True)
            Catch ex As Exception
                log4net.Config.XmlConfigurator.Configure()
                log.Error("Error error " & AddTabSpace(1) & Session("Username") & AddTabSpace(1) & "IP:" & GetIPAddress(), ex)
                Excute_Javascript("Alertbox('Cập nhật thất bại.');", Me.Page, True)
            End Try
        End Using
    End Sub

#End Region

#Region "Event for control "
    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Dim arrImage() As String = hidStrImage.Text.Trim.Split(",")
        Dim arrDest() As String = hidStrCommnet.Text.Trim.Split(";")

        If Session("SoTinThem") Is Nothing Or Session("SoTinThem") < 1 Then
            Excute_Javascript("Alertbox('Số tin bạn được thêm đã hết, vui lòng nâng cấp tài khoản hoặc liên hệ quản trị để được hỗ trợ')", Me.Page)
            Exit Sub
            'Response.Redirect(MainDomain & "thong-bao-tin-dang.html")
        End If


        Using data As New VSEntities
            Dim p As New VSModel.RaoVat
            Try
                p.Title = ddlHangchetao.SelectedItem.Text & " " & ddlDoixe.SelectedItem.Text.Replace("|__", "") & " " & txtPhanHangModel.Text.Trim & "-" & ddlNamsanxuat.SelectedItem.Text
                p.QueryString = makeURLFriendly(p.Title)
                p.Description = txtThietBiKhacCoBan.Text.Trim()

                p.ImagePreview = hidImagePath.Text.Trim
                If rbnVND.Checked = True Then
                    p.OriginalPrice = Math.Round(Decimal.Parse(txtGiaBan.Text.Replace(",", "").Trim()), 0)
                    p.Currency = 0
                    'price in USD
                    p.SellPrice = Math.Round((Decimal.Parse(txtGiaBan.Text.Replace(",", "").Trim()) * 1000000) / TyGia, 0)

                Else
                    p.SellPrice = Math.Round(Decimal.Parse(txtGiaBan.Text.Replace(",", "").Trim()), 0)
                    p.Currency = 1
                    'price in USD
                    p.OriginalPrice = Math.Round((Decimal.Parse(txtGiaBan.Text.Replace(",", "").Trim()) * TyGia) / 1000000, 0)


                End If

                p.Created = Now
                p.CreatorId = Session("UserID")
                'luu cho tinh trang xe co anh

                p.IsSellOff = IIf(hidImagePath.Text <> "", True, False)

                'luu tinh trang xe true = xe moi, false = xe cu
                p.IsHot = IIf(ddlTinhTrang.SelectedValue = 1, True, False)

                'neu la vip thi xuat ban luon
                If Session("UserVip") = 2 Or Session("UserVip") = 3 Then
                    p.StatusId = 3
                    p.bEffectiveDate = Now
                    p.eEffectiveDate = Now.AddDays(30)
                    p.PublishedDate = Now
                Else
                    p.StatusId = 2
                End If


                p.CateID = ddlDoixe.SelectedValue
                p.UserId = Session("UserID")
                p.TinhTP = ddlKhuVuc.SelectedValue
                p.DiaChi = txtDiaChi.Text.Trim
                p.HoTen = txtHoTen.Text.Trim
                p.Phone = txtDienThoai.Text.Trim
                p.PhoneOther = txtDienThoaiKhac.Text.Trim

                data.RaoVats.AddObject(p)
                data.SaveChanges()
                If p.RaovatId > 0 Then
                    'generate Code;
                    p.Code = FormatCode(p.RaovatId.ToString)
                    Dim t As List(Of VSModel.RaovaImage) = (From h In data.RaovaImages Where h.RaoVatId = hidID.Value Select h).ToList
                    For Each h As RaovaImage In t
                        data.RaovaImages.DeleteObject(h)
                    Next
                    For i As Integer = 0 To arrImage.Length - 2
                        Dim b As New RaovaImage
                        b.Description = arrDest(i).ToString
                        b.ImageURL = arrImage(i).ToString
                        b.RaoVatId = p.RaovatId
                        data.RaovaImages.AddObject(b)
                    Next
                    data.SaveChanges()

                    'Insert Thong tin co ban
                    InsertThongTinCoBan(p.RaovatId)
                    'Insert AnToan
                    InsertAnToan(p.RaovatId)
                    'Insert Tien nghi
                    InsertTienNghi(p.RaovatId)
                    'Insert Ky thuat
                    InsertThongSoKyThuat(p.RaovatId)
                End If

                Insert_App_Log("Insert Raovat:" & txtTitle.Text.Trim & "", Function_Name.Raovat, Audit_Type.Create, Request.ServerVariables("REMOTE_ADDR"), Session("UserName"))
                Excute_Javascript("Alertbox('Cập nhật dữ liệu thành công.');window.location ='" + MainDomain + "my-oto.html';", Me.Page, True)
            Catch ex As Exception
                log4net.Config.XmlConfigurator.Configure()
                log.Error("Error error " & AddTabSpace(1) & Session("Username") & AddTabSpace(1) & "IP:" & GetIPAddress(), ex)
                Excute_Javascript("Alertbox('Cập nhật thất bại.');", Me.Page, True)
            End Try
        End Using
    End Sub

    Protected Sub btnHuy_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnHuy.Click
        Response.Redirect("my-oto.html")
    End Sub

#End Region

    Protected Sub ddlHangchetao_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlHangchetao.SelectedIndexChanged
        If ddlHangchetao.SelectedValue > 0 Then
            getDoiXe(ddlHangchetao.SelectedValue)

        End If
    End Sub
End Class
